package com.fatgirl.time
{
	import com.fatgirl.time.ITimerElement;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	/**
	 * 时间元素类 
	 * @author iceman
	 * 
	 */
	internal class TimerElement
	{
		private var m_timer:Timer;
		private var m_timeElement:ITimerElement;
		private var m_startTime:int;
		private var m_delay:int;
		public function TimerElement(_delay:int, _timeElement:ITimerElement, _repeatCount:int)
		{
			m_delay = _delay;
			m_timer = new Timer(_delay,_repeatCount);
			m_timeElement = _timeElement;
			m_timer.addEventListener(TimerEvent.TIMER,timerHandler);
			
		}
		public function start():void
		{
			m_timer.start();
			m_startTime = getTimer();
		}
		public function stop():void
		{
			m_timer.stop();
		}
		public function reset():void
		{
			m_timer.reset();
			m_startTime = getTimer();
		}
		public function remove():void
		{
			m_timer.stop();
			m_timer.removeEventListener(TimerEvent.TIMER,timerHandler);
			m_timer = null;
		}
		public function get running():Boolean
		{
			if(m_timer)
			{
				return m_timer.running;
			}
			else
			{
				return false;
			}
		}
			
		private function timerHandler(e:TimerEvent):void
		{
			
			var t_timeError:int = getTimer() - m_startTime - m_delay;
			m_timeElement.timeExecute(t_timeError);
			m_startTime = getTimer();
		}
	}
}